package _binary_search;

import java.util.Arrays;

/**
 * 1099. 小于 K 的两数之和
 */
public class No1099 {
    public int twoSumLessThanK(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;
        int left = 0, right = n - 1;
        int answer = -1;
        while (left < right) {
            if (nums[left] + nums[right] >= k) right--;
            else {
                answer = Math.max(nums[left] + nums[right], answer);
                left++;
            }
        }
        return answer;
    }
}
